/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 * <p>
 * <p>
 * file description.
 *
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-07-30              zhihuazhang         Create it.
 * <p>
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-07-30              zhihuazhang         Create it.
 */

/**
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-07-30              zhihuazhang         Create it.
 *
 */

package com.cart.musicdemo.base.utils;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;

import java.io.File;
import java.lang.reflect.Method;

@SuppressWarnings("all")
public class StorageUtils {
    public static String getUsbStoragePath(Context context) {
        StorageManager storageManager =
            (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        for (StorageVolume volume : storageManager.getStorageVolumes()) {
            if (volume.isRemovable() && Environment.MEDIA_MOUNTED.equals(volume.getState())) {
                try {
                    // 低版本API没有getDirectory()，对getPath()进行反射
                    Method getPath = StorageVolume.class.getDeclaredMethod("getPath");
                    return new File((String) getPath.invoke(volume)).getAbsolutePath();
                } catch (Exception exception) {
                    throw new RuntimeException(exception);
                }
            }
        }
        return null;
    }
}